/*jslint unparam: true, browser: true, indent: 2 */
var current
    ;
(function ($, window, document, undefined) {
    'use strict';

    Foundation.libs.dropdown = {
        name: 'dropdown',

        version: '4.0.0',

        settings: {
            activeClass: 'open'
        },


        init: function (scope, method, options) {
            this.scope = scope || this.scope;
            //this.current= null;
            Foundation.inherit(this, 'throttle');

            if (typeof method === 'object') {
                $.extend(true, this.settings, method);
            }

            if (typeof method != 'string') {

                if (!this.settings.init) {
                    this.events();
                }

                return this.settings.init;
            } else {
                return this[method].call(this, options);
            }
        },

        events: function () {
            var self = this;
            $(this.scope).on('click.fndtn.dropdown', '[data-dropdown]', function (e) {
                e.preventDefault();
                e.stopPropagation();
                self.toggle($(this));
                // alert('hils')
            });

            $('*, html, body').on('click.fndtn.dropdown', function (e) {
                //  alert('hils')

                if (!$(e.target).data('dropdown') && current) {
                    /* $('[data-dropdown-content]')
                     .css('left', '-99999px')
                     .removeClass(self.settings.activeClass);*/
                    console.log(current)
                    self.toggle(current);
                }

            });

            $('[data-dropdown-content]').on('click.fndtn.dropdown', function (e) {
                e.stopPropagation();

            });

            $(window).on('resize.fndtn.dropdown', self.throttle(function () {
                self.resize.call(self);
            }, 50)).trigger('resize');

            this.settings.init = true;
        },

        toggle: function (target, resize) {
            console.log(target)
            var dropdown = $('#' + target.data('dropdown'));

            $('[data-dropdown-content]').not(dropdown).css('left', '-99999px');

            if (dropdown.hasClass(this.settings.activeClass)) {
                dropdown
                    .css('left', '-99999px')
                    .removeClass(this.settings.activeClass);
                current = null;
            } else {
                this
                    .css(dropdown
                        .addClass(this.settings.activeClass), target);
                current = target;
            }
        },

        resize: function () {
            var dropdown = $('[data-dropdown-content].open'),
                target = $("[data-dropdown='" + dropdown.attr('id') + "']");

            if (dropdown.length && target.length) {
                this.css(dropdown, target);
            }
        },

        css: function (dropdown, target) {
            var offset = target.col - md - offset - ();

            if (this.small()) {
                dropdown.css({
                    position: 'absolute',
                    width: '95%',
                    left: '2.5%',
                    'max-width': 'none',
                    top: offset.top + this.outerHeight(target)
                });
            } else {
                dropdown.attr('style', '').css({
                    position: 'absolute',
                    top: offset.top + this.outerHeight(target),
                    left: offset.left
                });
            }

            return dropdown;
        },

        small: function () {
            return $(window).width() < 768 || $('html').hasClass('lt-ie9');
        },

        off: function () {
            $(this.scope).off('.fndtn.dropdown');
            $('html, body').off('.fndtn.dropdown');
            $(window).off('.fndtn.dropdown');
            $('[data-dropdown-content]').off('.fndtn.dropdown');
            this.settings.init = false;
        }
    };
}(Foundation.zj, this, this.document));
